package com.sk.weichat.ui.other;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.qrcode.utils.CommonUtils;
import com.sk.weichat.AppConfig;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.message.GroupMessage;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.db.dao.FriendDao;
import com.sk.weichat.db.dao.GroupMessageDao;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.base.BaseLoginActivity;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.FileUtil;
import com.sk.weichat.view.HeadView;
import com.sk.weichat.view.MessageAvatar;

/**
 * Created by Administrator on 2017/9/14 0014.
 * 二维码类
 */
public class QRcodeActivity extends BaseLoginActivity {
    private ImageView qrcode;
    private HeadView mPAva;
    private boolean isgroup;
    private boolean isopengroup;
    private String group_name;
    private String group_image;

    private String userId;
    private String roomJid;
    private String action;
    private String str;

    private Bitmap bitmap;
    private ImageView mImgCycleAvatar;
    private MessageAvatar mImgCycleGroup;
    private TextView mTvNickname;
    private TextView mTvDescribe;
    String packetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im_code_image);
        if (getIntent() != null) {
            packetId = getIntent().getStringExtra("packetId");
            isgroup = getIntent().getBooleanExtra("isgroup", false);
            userId = getIntent().getStringExtra("userid");
            if (isgroup) {
                isopengroup = getIntent().getBooleanExtra("isopengroup", false);
                if (isopengroup) {
                    group_name = getIntent().getStringExtra("group_name");
                    group_image = getIntent().getStringExtra("group_image");
                }
                roomJid = getIntent().getStringExtra("roomJid");
            }
        }
        initActionBar();
        initView();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.mergerStatus).setBackgroundColor(Color.parseColor("#00000000"));
        ImageView mIvTitleLeft = (ImageView) findViewById(R.id.iv_title_left);
        mIvTitleLeft.setImageResource(R.mipmap.ic_back_white);
        mIvTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvTitle = (TextView) findViewById(R.id.tv_title_center);
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
        tvTitle.setText(getString(R.string.qrcode_card));
        ImageView mIvTitleRight = (ImageView) findViewById(R.id.iv_title_right);
        mIvTitleRight.setImageResource(R.drawable.icon_download_white);
        mIvTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap QRbitmap = FileUtil.getActivityDecorView(QRcodeActivity.this);
                FileUtil.saveImageToGallery2(mContext, QRbitmap);
            }
        });
    }

    private void initView() {
        qrcode = (ImageView) findViewById(R.id.qrcode);
        mPAva = findViewById(R.id.avatar_img);
        mImgCycleAvatar = (ImageView) findViewById(R.id.img_cycle_avatar);
        mImgCycleGroup = (MessageAvatar) findViewById(R.id.img_cycle_group);
        mTvNickname = findViewById(R.id.tv_qrcode_nickname);
        mTvDescribe = findViewById(R.id.tv_qrcode_describe);

        if (isgroup) {
            action = "group";
            mPAva.setVisibility(View.VISIBLE);
            mImgCycleGroup.setVisibility(View.VISIBLE);
            mTvDescribe.setText(getString(R.string.group_qrcode_describe));
        } else {
            action = "user";
            mPAva.setVisibility(View.VISIBLE);
            mImgCycleAvatar.setVisibility(View.VISIBLE);
            mTvNickname.setText(coreManager.getSelf().getNickName());
            mTvDescribe.setText(getString(R.string.person_qrcode_describe));
        }
        str = coreManager.getConfig().website + "?action=" + action + "&shikuId=" + userId;
        Log.e("zq", "二维码链接：" + str);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;

        // 生成二维码
        bitmap = CommonUtils.createQRCode(str, screenWidth, screenWidth);

        // 显示 二维码 与 头像
        if (isgroup) {// 群组头像
            if (isopengroup) {
                mTvNickname.setText(group_name);
                String url = coreManager.readConfigBean().getDownloadAvatarUrl() + group_image;
                if (TextUtils.isEmpty(group_image)) {
                    GroupMessage mGroupMessage = GroupMessageDao.getInstance().getGroupMessage(coreManager.getSelf().getUserId(), packetId);
                    AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mPAva.getHeadImage());
                    AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgCycleGroup.getHeadImage());
                } else {
                    AvatarHelper.getInstance().displayUrl(coreManager.readConfigBean().getDownloadAvatarUrl() + group_image, mImgCycleGroup.getHeadImage(), R.drawable.groupdefault);
                    AvatarHelper.getInstance().displayUrl(coreManager.readConfigBean().getDownloadAvatarUrl() + group_image, mPAva.getHeadImage(), R.drawable.groupdefault);
                }

            } else {
                Friend friend = FriendDao.getInstance().getFriend(coreManager.getSelf().getUserId(), roomJid);
                if (friend != null) {
                    mImgCycleGroup.fillData(friend);
//                AvatarHelper.getInstance().displayAvatar(friend.getUserId(), mPAva);
                    AvatarHelper.getInstance().displayAvatar(friend.getUserId(), friend, mPAva);
                    mTvNickname.setText(friend.getNickName());
                }
            }
        } else {// 用户头像
            AvatarHelper.getInstance().displayAvatar(userId, mPAva);
            AvatarHelper.getInstance().displayAvatar(userId, mImgCycleAvatar);

        }
        //将原始图片缩放成ImageView控件的高宽
//        Bitmap zoomBitmap = AppUtils.zoomBitmap(bitmap,w, h);
//        imageView.setImageBitmap(bitmap);
//        qrcode.setImageBitmap(zoomBitmap);
        BitmapDrawable bd = new BitmapDrawable(getResources(), bitmap);
        qrcode.setBackground(bd);
    }

    /**
     * 获取这个view的缓存bitmap,
     */
    private Bitmap getBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap result = Bitmap.createBitmap(view.getDrawingCache());
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(false);
        return result;
    }
}